defaults:

  update-submodules: &update-submodules
    run:
      name: "Update git submodules"
      command: |
        git submodule update --init

  environment-info: &environment-info
    run:
      name: "Environment info"
      command: |
        echo CXX: $CXX
        echo Generator: $GENERATOR
        echo CMake options: $CMAKE_OPTIONS
        $CXX --version
        $CXX --version > compiler.version

  configure: &configure
    run:
      name: "Configure"
      # Build "out-of-source" to have better coverage report
      # (ninja is using relative paths otherwise).
      working_directory: ~/build
      command: |
        cmake ../project -G "$GENERATOR" -DCMAKE_BUILD_TYPE=$BUILD_TYPE $CMAKE_OPTIONS

  build: &build
    run:
      name: "Build"
      working_directory: ~/build
      command: cmake --build . -- -j $BUILD_PARALLEL_JOBS

  store-hera: &store-hera
    store_artifacts:
      path: ~/build/src/libhera.so
      destination: libhera.so

  save-deps-cache: &save-deps-cache
    cache-save:
      name: "Save dependencies cache"
      key: &deps-cache-key deps-4-{{arch}}-{{checksum "compiler.version"}}-{{checksum "cmake/ProjectBinaryen.cmake"}}
      paths:
        - ~/build/deps

  restore-deps-cache: &restore-deps-cache
    cache-restore:
      name: "Restore dependencies cache"
      key: *deps-cache-key

  save-eth-cache: &save-eth-cache
    save_cache:
      key: &eth-cache-key cpp-prebuilt-cache-{{arch}}-{{checksum "compiler.version"}}-b387b5713738da12444af8d3a0f6390f2c87b76e
      paths:
        - ~/build
        - ~/.hunter

  restore-eth-cache: &restore-eth-cache
    restore_cache:
      key: *eth-cache-key

  checkout-cpp-eth: &checkout-cpp-eth
    run:
      name: "Checkout cpp-ethereum repo"
      working_directory: ~/project
      command: |
        cd ..
        git clone https://github.com/ethereum/cpp-ethereum --branch develop --single-branch
        cd cpp-ethereum
        git reset --hard b387b5713738da12444af8d3a0f6390f2c87b76e
        git submodule update --init

  link-hera: &link-hera
    run:
      name: "Link hera to prebuilt cpp-eth"
      working_directory: ~/project
      # TODO: run cmake in hera dir with config flags -DHERA_DEBUGGING=ON -DHERA_EVM2WASM_JS=ON
      command: |
        cd ..
        echo "pwd: $(pwd)"
        echo "listing files: $(ls -al)"
        rm -rf ~/cpp-ethereum/hera
        ln -s ~/project ~/cpp-ethereum/hera

  build-cpp-eth: &build-cpp-eth
    run:
      name: "Build cpp-ethereum with linked Hera"
      working_directory: ~/project
      # export PATH is an alternative to `make install`
      command: |
        cd ..
        mkdir -p build && cd build
        cmake ../cpp-ethereum -DCMAKE_BUILD_TYPE=RelWithDebInfo -DHERA=ON
        make -j8
        echo "build finished."
        echo 'export PATH=~/build/test:$PATH' >> $BASH_ENV
        cd ../cpp-ethereum
        echo "cpp-ethereum branch: $(git rev-parse --abbrev-ref HEAD)  commit: $(git rev-parse HEAD)"
        cd hera
        echo "hera branch: $(git rev-parse --abbrev-ref HEAD)  commit: $(git rev-parse HEAD)"

  test-ewasm: &test-ewasm
    run:
      name: "Run ewasm state tests"
      working_directory: ~/project
      command: |
        echo "print working directory:"
        pwd
        echo "list files in working directory:"
        ls -al .
        echo "check testeth command"
        testeth --help
        echo "run tests script."
        ./scripts/tests.sh

  test-evm2wasm: &test-evm2wasm
    run:
      name: "Run evm2wasm state tests"
      working_directory: ~/project
      command: |
        echo "print working directory:"
        pwd
        echo "list files in working directory:"
        ls -al .
        echo "check testeth command"
        testeth --help
        echo "run tests script."
        ./scripts/evm2wasm-tests.sh

  install-cpp-ethereum: &install-cpp-ethereum
    run:
      name: "Install cpp-ethereum"
      command: |
        URL=https://github.com/ethereum/cpp-ethereum/releases/download/v1.4.0.dev0/cpp-ethereum-1.4.0.dev0-linux.tar.gz
        curl -L $URL | sudo tar xz -C /usr/local

  fetch-tests: &fetch-tests
    run:
      name: "Fetch eWASM tests"
      command: |
        git clone https://github.com/ewasm/tests -b wasm-tests --single-branch --depth 1

  test: &test
    run:
      name: "Test shared Hera"
      command: |
        testeth --version
        testeth -t GeneralStateTests/stEWASMTests -- --testpath tests --vm ~/build/src/libhera.so --singlenet Byzantium


version: 2
jobs:

  # to run this using local circleci tool, rename Ewasm-Tests to `build` then do `circleci build -c circle.yml`
  ewasm-tests:
    environment:
      - BUILD_TYPE: Release
      - CXX: clang++
      - CC:  clang
      - GENERATOR: Ninja
      - BUILD_PARALLEL_JOBS: 4
      - CMAKE_OPTIONS: -DHERA_DEBUGGING=OFF
    docker:
      - image: ethereum/cpp-build-env
    steps:
      - checkout
      - *update-submodules
      - *environment-info
      - *checkout-cpp-eth
      - *restore-eth-cache
      - *link-hera
      - *build-cpp-eth
      - *save-eth-cache
      - *test-ewasm

  evm2wasm-tests:
    environment:
      - BUILD_TYPE: Release
      - CXX: clang++
      - CC:  clang
      - GENERATOR: Ninja
      - BUILD_PARALLEL_JOBS: 4
      - CMAKE_OPTIONS: -DHERA_DEBUGGING=OFF
    docker:
      - image: cdetrio/nodejs-cpp-build-env
    steps:
      - checkout
      - *update-submodules
      - *environment-info
      - *checkout-cpp-eth
      - *restore-eth-cache
      - *link-hera
      - *build-cpp-eth
      - *save-eth-cache
      - *test-evm2wasm

  linux-clang-shared-release:
    environment:
      - BUILD_TYPE: Release
      - CXX: clang++
      - CC:  clang
      - GENERATOR: Ninja
      - BUILD_PARALLEL_JOBS: 4
      - CMAKE_OPTIONS: -DBUILD_SHARED_LIBS=ON -DHERA_DEBUGGING=OFF
    docker:
      - image: ethereum/cpp-build-env
    steps:
      - checkout
      - *update-submodules
      - *environment-info
      - *restore-deps-cache
      - *configure
      - *build
      - *save-deps-cache
      - *install-cpp-ethereum
      - *store-hera
      - *fetch-tests
      - *test

  linux-clang-shared-debug:
    environment:
      - BUILD_TYPE: Debug
      - CXX: clang++
      - CC:  clang
      - GENERATOR: Ninja
      - BUILD_PARALLEL_JOBS: 4
      - CMAKE_OPTIONS: -DBUILD_SHARED_LIBS=ON -DHERA_DEBUGGING=ON
    docker:
      - image: ethereum/cpp-build-env
    steps:
      - checkout
      - *update-submodules
      - *environment-info
      - *restore-deps-cache
      - *configure
      - *build
      - *save-deps-cache
      - *install-cpp-ethereum
      - *store-hera
      - *fetch-tests
      - *test

  linux-gcc-debug:
    environment:
      - BUILD_TYPE: Debug
      - CXX: g++
      - CC:  gcc
      - GENERATOR: Unix Makefiles
      - BUILD_PARALLEL_JOBS: 4
      - CMAKE_OPTIONS: -DHERA_DEBUGGING=ON
    docker:
      - image: ethereum/cpp-build-env
    steps:
      - checkout
      - *update-submodules
      - *environment-info
      - *restore-deps-cache
      - *configure
      - *build
      - *save-deps-cache

workflows:
  version: 2
  hera:
    jobs:
      - linux-clang-shared-release
      - linux-clang-shared-debug
      - linux-gcc-debug
      - ewasm-tests
      - evm2wasm-tests
